<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Farmacias_Model
 *
 * @author marcelo.diaz
 */
//class Farmacias_Model extends Model{
class Farmacias_Model extends Model{
  function Farmacias_Model(){
    parent::Model(7);
  }

  /**
   * Retrieve all open Pharmacies according to current date   
   */
  function get_deturno(){  	
  	 $query = $this->db->query("SELECT * 
  	 							FROM deturno
  	 							LEFT OUTER JOIN marker as m ON deturno.farmacia = m.id_marker
  	 							WHERE (NOW() BETWEEN deturno.fecha_inicio AND deturno.fecha_fin) AND
  	 								  m.aproved = 1
  	 							");
	 $places = $query->result_array();
	 foreach ($places as $k => $data){
	 	//we MUST detect if icon and image are local, and add them the correct absolute path
	 	$places[$k]['icon'] = base_url()."images/icons/".$data['icon'];
	 	if(strpos($data['img'], ':') === false){
	 		$places[$k]['img'] = $places[$k]['icon'];
	 	}
	 }	 	 
     return $places;
  }

  function _all(){
  	$query = $this->db->query("SELECT *
  			FROM marker
  			WHERE marker.marker_cat = $this->category AND
  					aproved = 1
  			ORDER BY marker.name
  			");
  			$places = $query->result_array();
  			foreach ($places as $k => $data){
  	 	//we MUST detect if icon and image are local, and add them the correct absolute path
  		$places[$k]['icon'] = base_url()."images/icons/".$data['icon'];
  		if(strpos($data['img'], ':') === false){
  		$places[$k]['img'] = $places[$k]['icon'];
  	}
  	}
  	return $places;
  	}
  
  	/**
  	*
  	* @param String $place: name of the local bussines to search
  	*/
  	function get_place($place){
  	$place = mysql_real_escape_string($place);
  	$related = explode('-', $place);
  	$query = $this->db->query("SELECT *
					  			FROM marker
					  			WHERE marker.marker_cat = $this->category AND
					  				 aproved = 1 AND
					  				 marker.name like '$place'
  							");
  	//$row = $query->row();
  	$row = $query->row_array();
  	if(empty($row)){
  		return false;
  	}else{
	  	//we MUST detect if icon and image are local, and add them the correct absolute path
	  	$row['icon'] = base_url()."images/icons/".$row['icon'];
	  		if(strpos($row['img'], ':') === false){
	  		$row['img'] = $row['icon'];
	  	}
	  	//get Related items
	  	foreach ($related as $k=>$w){
	  		$related[$k] = "marker.name like '%".$w."%'";
	  	}
	  	$where = implode(" OR ", $related);
	  	$query = $this->db->query("SELECT *
	  		FROM marker
	  		WHERE ($where) AND
	  			  aproved = 1 AND
	  				(marker.name NOT LIKE '$place')
	  		ORDER BY marker.name
	  		");
	  		$relates = $query->result_array();
	  		//we MUST detect if icon and image are local, and add them the correct absolute path
	  		foreach ($relates as $k => $v){
		  		$relates[$k]['icon'] = base_url()."images/icons/".$v['icon'];
		  		if(strpos($v['img'], ':') === false){
		  			$relates[$k]['img'] = $relates[$k]['icon'];
		  		}
	  		}
	  		$row['related'] = $relates;
	  		return $row;
  	}
  	}
}
?>
